package Init;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import DataTags.DataTagsAdapter;
import DataTags.files;
import Factory.SFac;
import Main.main;
import Sources.expdef;
import Sources.sourcesdef;
import System.Classifier;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Init implements Runnable{
	private static Init instance = null;
	public float count;
	public int state=0;
	private ProgressBar progressBar1;
	private Shell sShell = null;
	String sourcesQ="SELECT s FROM sourcesdef s";
	String filesQ="SELECT f FROM files f WHERE f.path LIKE :rep1";
	String expQ="SELECT e FROM expdef e WHERE e.sID=:rep1";
	LinkedList<files> totag=new LinkedList<files>();
	DataTagsAdapter DTA=main.menu.search.getDataTagsAdapter();
	public static Init getInstance() {
		if(instance == null) {
			instance = new Init();
		}
		return instance;
	}
	protected Init() {}
	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		Display display=new Display();
		sShell = new Shell(display,0);
		FillLayout sShellLayout = new FillLayout(org.eclipse.swt.SWT.HORIZONTAL);
		sShell.setBounds(405, 393, 222, 10);
		//sShell.setSize(222, 30);
		sShell.setLayout(sShellLayout);
		{
			progressBar1 = new ProgressBar(sShell, SWT.NONE);
			progressBar1.setBounds(0, 0, 222, 10);
		}
	}
	
	@SuppressWarnings("unchecked")
	public void run() {
		try {
			createSShell();
			sShell.open();
			Thread.sleep(1);
			SessionFactory factory=SFac.get();
			Session session=factory.openSession();
			Query queryS=session.createQuery(sourcesQ);
			List<sourcesdef> sourcesdefs=queryS.list();
			//go over all sources and determine which files to tag
			for (sourcesdef sourcesdef1 : sourcesdefs) {
				Query queryE=session.createQuery(expQ).setParameter("rep1",sourcesdef1.getID());
				List<expdef> expdefs=queryE.list();
				String npath = null;
				try{
					npath=sourcesdef1.getPath().replace('\\', '%');
				}catch (Exception e) {e.printStackTrace();}
				Query queryF=session.createQuery(filesQ).setParameter("rep1",npath);
				List<files> fileslist=queryF.list();
				//determine which files we need to tag according to exp values
				for (files files1 : fileslist) {
					for (expdef expression : expdefs) {
						//check for wildcards
						if (expression.getExp().contains("*")) {
							//remove them
							String r = "";
							String exp=new String(expression.getExp());
							for (int i = 0; i < exp.length(); i ++) {
								if (exp.charAt(i) !='*') r += exp.charAt(i);
							}
							exp=r;
							if(files1.getName().contains(exp)){
								totag.add(files1);
								break;
							}
						}
						//no wildcards
						else {
							if (files1.getName().equals(expression.getExp())) {
								totag.add(files1);
								break;
							}
						}
					}
				}
			}
			//we got a list of files we need to tag
			LinkedList<Classifier>classifiers=main.listener.getClassifiers();
			count=totag.size()*classifiers.size();
			int i=0;
			for (files files1 : totag) {
				for(Classifier classi: classifiers){
					main.menu.log.update("autotagging: "+files1.getName()+" at: "+files1.getPath());
					String[] tags=classi.getTags(new File(files1.getPath()));
						DTA.addTagsTo(files1.getName(), 
								files1.getLast_m(), 
								files1.getPath(),
							    tags);
						i++;
						state=getState(i, count);
						setProgressBar1(state);	
				}
			}
			//setProgressBar1(100);
			sShell.close();
			main.menu.log.update("Init Finished");
			session.flush();
			session.close();
		} catch (Exception e) {
			MessageBox messageBox = new MessageBox(sShell, SWT.OK);
    		messageBox.setMessage("Init is Terminated");
    		messageBox.open();
    		sShell.close();
		}
	}

	protected void  setProgressBar1(int state1){
		progressBar1.setSelection(state1);
		progressBar1.redraw();
		if(state1==100){
			state=100;
			progressBar1.setSelection(100);
			MessageBox messageBox = new MessageBox(sShell, SWT.OK);
    		messageBox.setMessage("Init is Complete");
    		if (messageBox.open()==SWT.OK){
    			progressBar1.setSelection(0);
    			state=0;
    		}
		}
	}
	protected int getState(float done,float percent) {
		float cur=done/percent;
		cur=cur*100;
		cur=cur-(cur%1);
		return (int)cur;
	}

}
